#!/usr/bin/env ruby

require 'bundler'
Bundler.setup
require File.expand_path('../../lib/rabbit_jobs', __FILE__)

class MyCurrentJob
  include RJ::Job
  queue :mandarin
  def perform(count)
  end
end

RJ.configure { |c|
  c.queue "mandarin", durable: true, auto_delete: false, manual_ack: true, arguments: {'x-ha-policy' => 'all'}
  c.server "amqp://localhost/rj"
}

shutdown = false
trap('INT') do
  shutdown = true
end

i = 0
loop do
  puts "publishing job #{i}"
  begin
    # RJ.publish_to(:mandarin, MyCurrentJob, i)
    MyCurrentJob.perform_async(i)
  rescue Exception => e
    puts e.message
    puts e.backtrace.join("\r\n")
  end

  if shutdown
    puts "closing connection."
    RJ::Publisher.send(:connection).stop
    break
  end

  i += 1
  sleep 1
end